package com.wg.elasticsearch.controller;

import com.wg.elasticsearch.constant.ElasticsearchConstants;
import com.wg.elasticsearch.model.ShowContent;
import com.wg.elasticsearch.param.ContentParam;
import com.wg.elasticsearch.result.ResponseData;
import com.wg.elasticsearch.result.ResultSupport;
import com.wg.elasticsearch.service.ElasticsearchService;
import com.wg.elasticsearch.service.ShowContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mc on 18/6/22.
 */
@RestController
@RequestMapping(value = "/es")
@Api(tags = "es", description = "es")
public class EsController {

    @Autowired
    private ElasticsearchService elasticsearchService;

    @Autowired
    private ShowContentService showContentService;

    @PostMapping("/init")
    @ApiOperation("init")
    public String init() {
        elasticsearchService.createIndex(ElasticsearchConstants.ES_JEST_INDEX_NAME);
        List<ShowContent> showContents = showContentService.getAll();
        if(showContents.size() <= 0){
           return ResponseData.toFail("数据库不存在数据");
        }
        boolean mappingPushData = elasticsearchService.createMappingPushData(ElasticsearchConstants.ES_JEST_INDEX_NAME, ElasticsearchConstants.ES_JEST_TYPE_NAME, elasticsearchService.getSourceName(), getList(showContents));
        if(mappingPushData) {
            return ResponseData.toSuccess();
        }else {
            return ResponseData.toFail("初始化失败");
        }
    }


    @PostMapping("/getTime")
    @ApiOperation("getTime")
    public String getTime() {
       return ResponseData.toSuccess(elasticsearchService.getMaxUpdateTime());
    }


    @PostMapping("/getById")
    @ApiOperation("getById")
    public String getById() {
        return ResponseData.toSuccess(elasticsearchService.get(ElasticsearchConstants.ES_JEST_INDEX_NAME, ElasticsearchConstants.ES_JEST_TYPE_NAME,"1"));
    }


    @PostMapping("/search")
    @ApiOperation("search")
    public String search(ContentParam param) {
        ResultSupport<List<ShowContent>> result = elasticsearchService.search(param);
        if(result.isSuccess()){
            Map<String, Object> resumeMap = new HashMap<>();
            resumeMap.put("showContents", result.getModule());
            resumeMap.put("totalCount", result.getTotalCount());
            return ResponseData.toSuccess(resumeMap);
        }else {
            return ResponseData.toFail("接口异常");
        }

    }

    /**
     * 获取object
     * @param list
     * @return
     */
    public List<Object> getList(List<ShowContent> list){
        List<Object> list2 = new ArrayList<>();
        for(ShowContent showContent : list){
            list2.add(showContent);
            }
        return list2;
    }
}

